//
//  RGXTwoController.m
//  JiCaiBao
//
//  Created by rgx on 16/10/21.
//  Copyright © 2016年 rgx. All rights reserved.
//

#import "RGXTwoController.h"
#import "AwardCell.h"
#import "MJRefresh.h"
#import "awardData.h"
#import "RGXHistoryAwardController.h"
@interface RGXTwoController (){
    
    NSMutableArray *prizeArr;   //各彩种最近一期开奖数据
}

@end

@implementation RGXTwoController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.awardTableView.delegate = self;
    self.awardTableView.dataSource = self;
    // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadNewData方法）
    self.awardTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    
    // 马上进入刷新状态
    [self.awardTableView.mj_header beginRefreshing];
    [self queryLastPrize];
}

- (void)loadNewData{
    
    [self.awardTableView.mj_header endRefreshing];
}

#pragma mark  -- 最近一期开奖信息
- (void)queryLastPrize{
    __weak RGXTwoController *selfInfo = self;
    
    
    NSMutableDictionary *body = [NSMutableDictionary dictionary];
    [body setObject:@"SSQ,HBP3,D3,QLC" forKey:@"gameIdList"]; //彩种名称，多个用逗号隔开
    
    AFSecurityPolicy *securityPolicy = [[AFSecurityPolicy alloc] init];
    [securityPolicy setAllowInvalidCertificates:YES];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    [manager POST:QueryLastPrize parameters:body progress:^(NSProgress * uploadProgress) {
        
    } success:^(NSURLSessionDataTask * task, id responseObject) {
        
        // 请求成功
        if(responseObject){
            RGXLog(@"responseObject: 最近一期开奖信息= %@",responseObject);
            [selfInfo handleLastPrizeData:responseObject];
        } else {
            RGXLog(@"无数据");
        }
        
    } failure:^(NSURLSessionDataTask * task, NSError * error) {
        RGXLog(@"error = %@",error);
    }];
}

- (void)handleLastPrizeData:(NSDictionary *)data{
    if (![data isKindOfClass:[NSDictionary class]]) {
        return;
    }
    if (![[data objectForKey:@"statusCode"] isEqualToString:@"0000"]) {
        return;
    }
    NSArray *listArr = [data objectForKey:@"body"];
    if (listArr.count == 0) {
        return;
    }
    if(!prizeArr){
        prizeArr = [[NSMutableArray alloc]init];
    }else{
        [prizeArr removeAllObjects];
    }
    for (NSDictionary *dict in listArr) {
        awardData *data = [[awardData alloc]init];
        data.lotteryId = [dict objectForKey:@"SSQ"];
        data.lotteryIssue = [dict objectForKey:@"issueNo"];
        data.lotteryTime = [self getTimeStr:[dict objectForKey:@"prizeTime"]];
        data.prizeBalls = [dict objectForKey:@"prizeBalls"];
        [prizeArr addObject:data];
    }
    [self.awardTableView reloadData];
}
#pragma UITableView delegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *strid =@"awardCell";
    AwardCell *cell = [tableView dequeueReusableCellWithIdentifier:strid];
    if(cell ==nil){
        
        NSArray *arr = [[NSBundle mainBundle]loadNibNamed:@"AwardCell" owner:self options:nil];
        cell = [arr lastObject];
    }
    awardData *data = [[awardData alloc]init];
    if (prizeArr) {
        data = [prizeArr objectAtIndex:indexPath.row];
        
    }
    [cell flashCell:data :(int)indexPath.row];
    
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
       //隐藏没用的cell
    tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    
    return cell;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 110.0;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    RGXHistoryAwardController *history = [[RGXHistoryAwardController alloc]initWithNibName:@"RGXHistoryAwardController" bundle:nil];
    if (indexPath.row == 0) {
        history.gameId = lottery_ssq_id;
    }else if (indexPath.row == 1){
        history.gameId = lottery_hbk3_id;

    }else if (indexPath.row == 2){
        history.gameId = lottery_fc3d_id;

    }else if (indexPath.row == 3){
        history.gameId = lottery_qlc_id;

    }
    [self.navigationController pushViewController:history animated:YES];
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
